package cn.school.controller.workflow;

import cn.school.classManage.vo.SchoolClassVO;
import cn.school.common.api.CommonPage;
import cn.school.common.api.CommonResult;
import cn.school.common.exception.ApiException;
import cn.school.student.entity.StudentInfo;
import cn.school.workflow.service.ViolativeApplyService;
import cn.school.workflow.vo.ViolativeApplyVO;
import cn.school.workflow.vo.ViolativeApplysVO;
import cn.school.workflow.vo.WorkFlowVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/violativeApply")
@Api(tags = "违纪反馈流程接口")
public class ViolativeApplyController {

    @Autowired
    private ViolativeApplyService violativeApplyService;
    /**
     * 查询流程部署信息
     * @return
     * @throws ApiException
     */
    @PostMapping("/findViolativeApplyPageList")
    @ApiOperation("list => 分页查询违纪申请单")
    public CommonResult<CommonPage<ViolativeApplyVO>> findViolativeApplyPageList(@RequestBody ViolativeApplyVO vo) throws ApiException{
        IPage<ViolativeApplyVO> pageList =  violativeApplyService.findViolativeApplyPageList(vo);
        return  CommonResult.success(CommonPage.restPage(pageList));
    }

    @PostMapping("/findViolativeApplyList")
    @ApiOperation("pageList => 分页查询违纪报表")
    public CommonResult<PageInfo> findViolativeApplyList(@RequestBody ViolativeApplysVO vo){
        return CommonResult.success(violativeApplyService.getList(vo));
    }

    @PostMapping("/cancelViolativeApply")
    @ApiOperation("cancel => 撤销违纪申请")
    public CommonResult<CommonPage<ViolativeApplyVO>> cancelViolativeApply(@RequestBody WorkFlowVO vo) throws ApiException{
        boolean flag =  violativeApplyService.cancelViolativeApply(vo);
        if(flag){
            return CommonResult.success();
        }
        return  CommonResult.failed("流程取消失败");
    }

    @PostMapping("/createViolativeApply")
    @ApiOperation("create => 创建违纪流程")
    public CommonResult<Integer> createViolativeApply(@RequestBody ViolativeApplyVO  vo) throws ApiException{

        int flag =  violativeApplyService.createViolativeApply(vo);
        if(flag>0){
            return CommonResult.success();
        }
        return  CommonResult.failed("流程创建失败");
    }

    @GetMapping("/findViolativeApplyDetail/{processInstanceId}")
    @ApiOperation("detail => 违纪详情")
    public CommonResult<ViolativeApplyVO> findViolativeApplyDetail(@PathVariable("processInstanceId") Long processInstanceId) throws ApiException{
        ViolativeApplyVO violativeApplyVO =  violativeApplyService.findViolativeApplyDetail(processInstanceId);
        if(violativeApplyVO!=null){
            return CommonResult.success(violativeApplyVO);
        }
        return  CommonResult.failed("查询不单申请单");
    }

    @PostMapping("/findClassAll")
    @ApiOperation("violativeProcess => 违纪处理通知")
    public CommonResult<List<SchoolClassVO>> findClassAll(@RequestBody ViolativeApplyVO  vo) throws ApiException{
        List<SchoolClassVO> classList = violativeApplyService.findClassAll();
        return  CommonResult.success(classList);
    }

    @GetMapping("/findStudentByClassId/{classId}")
    @ApiOperation("violativeProcess => 查询班级学生")
    public CommonResult<List<StudentInfo>> findStudentByClassId(@PathVariable("classId") String classId) throws ApiException{
        List<StudentInfo> studentList = violativeApplyService.findStudentByClassId(classId);
        return  CommonResult.success(studentList);
    }

    @PostMapping("/violativeProcess")
    @ApiOperation("violativeProcess => 违纪处理通知")
    @PreAuthorize("@ss.hasPermi('violative:apply:process')")
    public CommonResult<ViolativeApplyVO> violativeProcess(@RequestBody ViolativeApplyVO  vo) throws ApiException{
        int num = violativeApplyService.violativeProcess(vo);
        if(num>0){
            return CommonResult.success();
        }
        return  CommonResult.failed("提交失败");
    }
}
